package service

import (
	"gitee.com/gopher2011/mall/app/dao"
	"time"
)

// 全局公开对象
var SmsFlashPromotionSession = smsFlashPromotionSessionService{}

// 私有对象，所有的方法都绑定到这个结构体上。

type smsFlashPromotionSessionService struct{}

func (p *smsFlashPromotionSessionService) Create(arg dao.SmsFlashPromotionSession) (int64, error) {
	u := dao.SmsFlashPromotionSession{
		Name:       arg.Name,
		StartTime:  arg.StartTime,
		EndTime:    arg.EndTime,
		Status:     arg.Status,
		CreateTime: time.Now(),
	}
	return dao.SmsFlashPromotionSessionDao.InsertTemplate(u)
}

func (p *smsFlashPromotionSessionService) Update(id int64, arg dao.SmsFlashPromotionSession) (int64, error) {
	u := dao.SmsFlashPromotionSession{
		Id:        id,
		Name:      arg.Name,
		StartTime: arg.StartTime,
		EndTime:   arg.EndTime,
		Status:    arg.Status,
		//CreateTime: time.Now(),
	}
	return dao.SmsFlashPromotionSessionDao.UpdateTemplate(u)
}

func (p *smsFlashPromotionSessionService) UpdateStatus(id int64, status int32) (int64, error) {
	return dao.SmsFlashPromotionSessionDao.UpdateStatus(id, status)
}

func (p *smsFlashPromotionSessionService) Delete(id int64) (int64, error) {
	return dao.SmsFlashPromotionSessionDao.DeleteTemplate(id)
}

func (p *smsFlashPromotionSessionService) GetItem(id int64) (dao.SmsFlashPromotionSession, error) {
	return dao.SmsFlashPromotionSessionDao.SelectTemplate(id)
}

func (p *smsFlashPromotionSessionService) ListAll() ([]dao.SmsFlashPromotionSession, error) {
	return dao.SmsFlashPromotionSessionDao.ListAll()
}

func (p *smsFlashPromotionSessionService) List(flashPromotionId int64) ([]dao.SmsFlashPromotionSessionDetail, error) {
	list, err := dao.SmsFlashPromotionSessionDao.SelectByStatus(1)
	if err != nil {
		return nil, err
	}
	result := make([]dao.SmsFlashPromotionSessionDetail, 0)
	for _, v := range list {
		count, err := SmsFlashPromotionProductRelation.GetCount(flashPromotionId, v.Id)
		if err != nil {
			return nil, err
		}
		d := dao.SmsFlashPromotionSessionDetail{
			Id:           v.Id,
			Name:         v.Name,
			StartTime:    v.StartTime,
			EndTime:      v.EndTime,
			Status:       v.Status,
			CreateTime:   v.CreateTime,
			ProductCount: count,
		}
		result = append(result, d)
	}
	return result, nil
}
